package com.qf08;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 演示线程的创建
 * Executor:线程池的根接口,executor()
 *  ExecutorService:包含管理线程池的一些方法，submit,shutdown
 *      ThreadPoolExecutor
 *          ScheduledThreadPoolExecutor
 * Executors:创建线程池工具类
 *  （1）创建固定线程个数线程池
 *  （2）创建缓存线程池，由任务的多少决定
 *  （3）创建单线程池
 *  （4）创建调度线程池 调度:周期，定时执行
 *
 *
 *
 */
public class Demo1 {
    public static void main(String[] args) {
//        ExecutorService es = Executors.newFixedThreadPool(4);
//        ExecutorService es = Executors.newCachedThreadPool();
        ExecutorService es = Executors.newSingleThreadExecutor();
        Runnable runnable=new Runnable() {
            private int ticket=100;
            @Override
            public void run() {
                while(true){
                    if (ticket<=0){
                        break;
                    }
                    System.out.println(Thread.currentThread().getName()+"卖了第"+ticket+"张票");
                    ticket--;
                }
            }
        };
        //3.提交任务
        for (int i = 0; i < 4; i++) {
            es.submit(runnable);
        }
        es.shutdown();
    }
}
